﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DeepEarth.Core;
using DeepEarth.Map.Core;
using System.Collections.ObjectModel;
using DeepEarth.Map.Core.Tiling;

#if !WINDOWS_PHONE
using System.ComponentModel.Composition;
#endif

namespace DeepEarth.Map.TileSources
{
    /// <summary>
    /// A map mode describes a set of tiles and their opacity. It is used with the MapView Mode and SecondaryModes properties.
    /// </summary>

#if !WINDOWS_PHONE
    [Export(typeof(BaseTileSourceProvider))]
#endif
    public class NearMapTileSourceProvider : BaseTileSourceProvider<NearMapTileSourceFactory>
    {
        public NearMapTileSourceProvider()
            : this(NearMapTileSourceFactory.PhotoMap)
        {
        }

        public NearMapTileSourceProvider(MapMode mode)
            : base()
        {
            this.Mode = this.TileSourceFactory.ValidateAndCoerceMapMode(mode);
        }

    	bool _showStreets;
        public override bool ShowStreets
        {
            get { return _showStreets; }
            set
            {
                if (value == _showStreets) return;
                _showStreets = value;

                using (TileSourcesCollection.GetAtomicOperation())
                {
                    if (_showStreets)
                        TileSourcesCollection.Add(TileSourceFactory.CreateTileSource(NearMapTileSourceFactory.StreetMapOverlay));
                    else
                        TileSourcesCollection.RemoveAll(x => x.Mode == NearMapTileSourceFactory.StreetMapOverlay);

                    NotifyPropertyChanged(() => ShowStreets);
                }
            }
        }

		public override void OnLocationChanged(Location currentCentre)
		{
            //NearMapPhotoDate.GetDates(currentCentre)
            //    .ObserveOnDispatcher()
            //    .Subscribe(UpdateDates);
		}

		private void UpdateDates(IEnumerable<DateTime> dates)
		{
			DatesCollection.Clear();
			DatesCollection.AddRange(dates);
		}
    
		public override BaseTileSourceProvider Clone()
		{
			var result = new NearMapTileSourceProvider();
			FillClone(result);
			return result;
		}
	}
}